package com.byxy.managerial.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;
import com.byxy.managerial.dao.StaffDao;
import com.byxy.managerial.entity.Department;
import com.byxy.managerial.entity.Staff;
import com.byxy.managerial.service.StaffService;

/**
 * 员工service接口实现类
 *
 */
@Service
@Transactional
public class StaffServiceImpl implements StaffService {

	@Resource
	private StaffDao staffDao;

	@Override
	public boolean save(Staff staff) {
		if (staffDao.getByName(staff.getSid()) == null) {
			staffDao.save(staff);
			return true;
		}
		return false;
	}

	@Override
	public boolean delete(Integer id) {
		staffDao.delete(staffDao.getByID(id));
		return true;
	}

	@Override
	public boolean update(Staff staff) {
		if (staffDao.getByName(staff.getSid()) != null) {
			if (staffDao.getByName(staff.getSid()).getId() == staff.getId()) {
				staffDao.update(staff);// 如果要修改的员工所属编号的id与数据库所在id相符可以修改
				return true;
			} else {
				// 否则重复编号，不修改
				return false;
			}
		} else {
			// 编号不存在，可以修改
			staffDao.update(staff);
			return true;
		}

	}

	@Override
	public List<Staff> find() {
		return staffDao.find();
	}

	@Override
	public List<Staff> find(List<Department> list) {
		List<Staff> listStaff = new ArrayList<Staff>();
		if (list != null) {
			for (Department department : list) {
				for (Staff staff : department.getStaff()) {
					listStaff.add(staff);
				}
			}
		}
		return listStaff;
	}

	@Override
	public int countTotal() {
		return (int) staffDao.countAll();
	}

	@Override
	public Staff getByIdStaff(int id) {
		return staffDao.getByID(id);
	}

	@Override
	public List<Staff> find(int pos, int row) {
		return staffDao.find(pos, row);
	}

	@Override
	public List<Staff> findDim(String likeVal) {
		return staffDao.findDim(likeVal);
	}

}
